package com.imooc.es.controller;

import com.imooc.es.service.ItemSearchService;
import com.imooc.controller.BaseController;
import com.imooc.pojo.IMOOCJSONResult;
import com.imooc.pojo.PagedGridResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 顾志杰
 * @date 2020/9/7-14:56
 */
@RestController
@RequestMapping("search")
public class SearchController extends BaseController {

    private final ItemSearchService itemSearchService;

    @Autowired
    public SearchController(ItemSearchService itemSearchService) {
        this.itemSearchService = itemSearchService;
    }

    @ApiOperation(value = "根据es搜索商品列表", notes = "根据es搜索商品列表", httpMethod = "GET")
    @GetMapping("/items")
    public IMOOCJSONResult search(@ApiParam(name = "catId", value = "分类id", required = false)
                                  @RequestParam String keywords,
                                  @ApiParam(name = "sort", value = "排序方式", required = false)
                                  @RequestParam String sort,
                                  @ApiParam(name = "page", value = "页码", required = false)
                                  @RequestParam Integer page,
                                  @ApiParam(name = "pageSize", value = "页容", required = false)
                                  @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        page--;
        PagedGridResult comments = itemSearchService.seathItems(keywords, sort, page, pageSize);
        return IMOOCJSONResult.ok(comments);
    }


    @ApiOperation(value = "根据分类id搜索商品列表", notes = "根据分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public IMOOCJSONResult catItems(@ApiParam(name = "catId", value = "分类id", required = true)
                                    @RequestParam Integer catId,
                                    @ApiParam(name = "sort", value = "排序方式", required = false)
                                    @RequestParam String sort,
                                    @ApiParam(name = "page", value = "页码", required = false)
                                    @RequestParam Integer page,
                                    @ApiParam(name = "pageSize", value = "页容", required = false)
                                    @RequestParam Integer pageSize) {
        if (catId == null) {
            return IMOOCJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = PAGE;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult comments = itemSearchService.seathItems(catId, sort, page, pageSize);
        return IMOOCJSONResult.ok(comments);
    }
}
